/***
 * * PROJECT
 * *    ATaG
 * * VERSION
 * *    $Revision: 1.7 $
 * * DATE
 * *    $Date: 2007/08/17 20:19:36 $
 * * LOG
 * *    $Log: DataItem.java,v $
 * *    Revision 1.7  2007/08/17 20:19:36  animesh
 * *    removed DATAITEM_HELLOWORLDDATA
 * *
 * *    Revision 1.6  2007/08/17 20:10:39  animesh
 * *    moved serialization and deserialization of data items into the app package.
 * *
 * *    Revision 1.5  2007/06/21 17:27:27  animesh
 * *    thread start.. now we have a capacity problem.
 * *
 * *    Revision 1.4  2007/06/17 11:09:01  animesh
 * *    Serialization done. New Sensor and Actuator classes added.
 * *
 * *    Revision 1.3  2007/06/13 00:43:31  animesh
 * *    no message
 * *
 * *    Revision 1.2  2007/06/09 07:15:52  animesh
 * *    update after fixing all but sets and serialization.
 * *
 * *    Revision 1.4  2006/10/27 01:28:28  lmottola
 * *    Debugging support added.
 * *
 * *    Revision 1.3  2006/10/26 23:58:30  lmottola
 * *    Bug fixes: Hello World application running in SWANS.
 * *
 * *    Revision 1.2  2006/10/19 21:21:52  lmottola
 * *    Added CVS preamble.
 * *
 * *
 * *   ATaG (Abstract Task Graph) - Data-driven Macroprogramming for
 * *                                Sensor Networks
 * *
 * *   This program is free software; you can redistribute it and/or
 * *   modify it under the terms of the GNU General Public License
 * *   as published by the Free Software Foundation; either version 2
 * *   of the License, or (at your option) any later version.
 * *
 * *   This program is distributed in the hope that it will be useful,
 * *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 * *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * *   GNU General Public License for more details.
 * *
 * *   You should have received a copy of the GNU General Public License
 * *   along with this program; if not, you may find a copy at the FSF web
 * *   site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
 * *   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * *   Boston, MA  02111-1307, USA
 ***/

package atag.runtime;


import atag.j2mespecific.Serializable;
import java.io.IOException;

import java.util.Vector;

import atag.runtime.platformDependent.ArchConstants;
import java.io.DataInput;
import java.io.DataOutput;

public class DataItem implements Serializable{
    
    private String m_timeStamp;
    private String m_dateStamp;
    
    private int m_originID;
    private NodeLocation m_originLocation;
    //private Vector<Object> m_coreVector;
    private Vector m_coreVector;
    private int[] m_totalRefs;
    private int[] m_nowRefs;
    private int m_dataID;
    private int m_producerTaskID;
    
    public DataItem(){
        //blank - for deserialization
    }
    
    public DataItem(int t_ID, int t_producerTaskID, Object t_coreData) {
        m_coreVector = new Vector(1);
        m_coreVector.addElement(t_coreData);
        m_dataID = t_ID;
        m_producerTaskID = t_producerTaskID;
        m_totalRefs = new int[ArchConstants.MAXTASKS];
        m_nowRefs = new int[ArchConstants.MAXTASKS];
    }
    
    public Object core() {
        return m_coreVector.elementAt(0);
    }
    
    public void setOriginID(int t_origID) {
        m_originID = t_origID;
    }
    
    public void setOriginLocation(NodeLocation t_origLocation) {
        m_originLocation = t_origLocation;
    }
    
    public void setTimeStamp(String t_time) {
        m_timeStamp = t_time;
    }
    
    public void setDateStamp(String t_date) {
        m_dateStamp = t_date;
    }
    
    // Do the following 2 need corresponding getRef methods?
    public void setTotalReferences(int[] t_totalRefs) {
        m_totalRefs = t_totalRefs;
    }
    public void setNowReferences(int[] t_nowRefs) {
        m_nowRefs = t_nowRefs;
    }
    
    /** This is called only by getData.  Hence, nowRefs and totalRefs should both
     * be 1 for this index; the AtagManager should have set that before scheduling
     * this task that is calling the getData.  Hence check that both are 1.
     */
    public void decrementRef(int taskID) {
        if (m_totalRefs[taskID] != 1 || m_nowRefs[taskID] != 1) {
            System.err.println("total and now_refs are not both one. Why? task: " + taskID);
            System.exit(-1);
        } else {
            m_totalRefs[taskID]--;
            m_nowRefs[taskID]--;
        }
    }
    
    public int dataID() {
        return m_dataID;
    }
    
    public int producerID() {
        return m_producerTaskID;
    }
    
    public int originID() {
        return m_originID;
    }
    
    public NodeLocation originLocation() {
        return m_originLocation;
    }
    
    public String timeStamp() {
        return m_timeStamp;
    }
    
    public String dateStamp() {
        return m_dateStamp;
    }
    
    public boolean isActive() {
        for (int ctr=0; ctr<m_nowRefs.length; ctr++)
            if (m_nowRefs[ctr] == 1)
                return true;
        return false;
    }
    
    /** This is called by DataPool at the reqeust of AtagManager to check if
     * the item is 'available' so that a task could be scheduled.  Hence check
     * only totalRefs because nowRefs will typically be zero at this time.
     * Only after all isAlive calls are over with total and nowRefs be
     * initialized by the AtagManager, depending on the result of the calls.
     */
    
    public boolean isAlive(int taskID) {
        if (m_totalRefs[taskID] == 1)
            return true;
        else
            return false;
    }
    
    
    public void serialize(DataOutput dg) throws IOException {
        dg.writeUTF(this.m_timeStamp);
        
        dg.writeUTF(this.m_dateStamp);
        
        dg.writeInt(this.m_originID);
        
        this.m_originLocation.serialize(dg);
        
        // serializing the data private Vector m_coreVector;
        
        atag.apps.mainApp.gen.DataItemUtils.serializeData(dg, this.m_coreVector);
        
        dg.writeInt(this.m_totalRefs.length);
        for(int i=0; i<this.m_totalRefs.length;i++)
            dg.writeInt(this.m_totalRefs[i]);
        
        dg.writeInt(this.m_nowRefs.length);
        for(int j=0; j<this.m_nowRefs.length; j++)
            dg.writeInt(this.m_nowRefs[j]);
        
        dg.writeInt(this.m_dataID);
        
        dg.writeInt(this.m_producerTaskID);
        
    }
    
    
    public void deserialize(DataInput dg) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        this.m_timeStamp = dg.readUTF();
        
        this.m_dateStamp = dg.readUTF();
        
        this.m_originID = dg.readInt();
        
        this.m_originLocation = new NodeLocation();
        this.m_originLocation.deserialize(dg);
        
        // deserializing the data private Vector m_coreVector;
        this.m_coreVector = new Vector(1);
        
        //add elements to this vector using the DataItemUtils
        //The line below will show an error, until the templates inside
        //atag.apps.mainApp are created by Srijan.

        atag.apps.mainApp.gen.DataItemUtils.deserializeData(dg, this.m_coreVector);
        
        this.m_totalRefs = new int[dg.readInt()];
        for(int i=0; i<this.m_totalRefs.length;i++)
            this.m_totalRefs[i] = dg.readInt();
        
        this.m_nowRefs = new int[dg.readInt()];
        for(int j=0; j<this.m_nowRefs.length; j++)
            this.m_nowRefs[j] = dg.readInt();
        
        this.m_dataID = dg.readInt();
        
        this.m_producerTaskID = dg.readInt();
    }  
}
